package org.eerbaar.parser.core.exe;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SymbolList {

    private final List<SymbolInstance> list = new ArrayList<SymbolInstance>();

    public SymbolList() {
        //
    }

    public SymbolList(SymbolInstance[] symbols) {
        list.addAll(Arrays.asList(symbols));
    }

    public List<SymbolInstance> getList() {
        return this.list;
    }

    public List<SymbolInstance> getRange(int start, int end) {

        List<SymbolInstance> result = new ArrayList<SymbolInstance>();

        boolean in = false;
        for (SymbolInstance s : list) {

            if (s.getEnd() >= end) {
                in = false;
            }

            if (s.getStart() >= start) {
                in = true;
            }

            if (in) {
                result.add(s);
            }

        }

        return result;
    }

    public void replace(SymbolInstance symbol) {

        List<SymbolInstance> range = getRange(symbol.getStart(),
                symbol.getEnd());
        if (range.size() > 0) {

            int index = list.indexOf(range.get(0));
            list.removeAll(range);

            list.add(index, symbol);
        }
    }

    @Override
    public String toString() {
        return list.toString();
    }

}
